package com.sailorj.medical.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sailorj.medical.domain.menu.Menu;
import com.sailorj.medical.domain.role.Role;
import com.sailorj.medical.domain.user.User;
import com.sailorj.medical.exception.UserNotFoundException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @author sailorj
 */
@WebServlet(name = "LoginServlet") //等价于在web.xml配置
public class LoginServlet extends BaseServlet {

    private static final long serialVersionUID = 7279138098299110478L;
    private static final Logger LOG = LogManager.getLogger(LoginServlet.class);

    // 用户和Session绑定关系
    final Map<String, HttpSession> MAP1 = new HashMap<String, HttpSession>();
    // SessionId和用户的绑定关系
    final Map<String, String> MAP2 = new HashMap<String, String>();

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
             login(request, response);
    }

    private void login(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        String path = "login.jsp";
        List<Object> urlDataList = new ArrayList<Object>();
        List<Object> menuDataList = new ArrayList<Object>();

        try{
            String userid=request.getParameter("userid");
            String pwd   =request.getParameter("pwd");
            User user=new User(userid,pwd);

            if (user == null) {
                request.setAttribute("error", "用户名或密码错误！");
                process(request, response, path);
            }

            // 根据用户名获取权限路径url
            for (Role role : user.getRoles()) {
                for (Menu menu : role.getMenus()) {
                    urlDataList.add(menu.getUrl());
                    session.setAttribute("urlDataList", urlDataList);
                }
            }

            session.setAttribute("agencode", user.getAgencode()); // 在session中设置登记员的行政区域编号
            session.setAttribute("userid", user.getFullname()); // 在session中设置用户
            request.setAttribute("list", menuDataList);
            LOG.info(userid+"用户登录成功!!!");
            path="index.jsp";
        }catch(UserNotFoundException e){
            LOG.info("用户登录失败!!!");
            LOG.info(e.getMessage(),e);
            path="login.jsp";
        }
        //跳转页面
        process(request, response, path);

    }
}
